<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/3/11
 * Time: 11:03
 */

namespace Api\Domain;

use Admin\Api\AqApi;

class AqDomain extends BaseDomain{

    protected $business_code = 'aq';

    protected $allowType = array("json", "rss", "html");

    /**
     * 硬件设备添加
     */
    public function add(){

        $this->checkVersion(100);
        addLog("Aq/add",$_GET,$_POST,"应用" . $this->client_id . "调用Aq添加接口");

        $uid = $this->_post('uid','','用户ID缺失');
        $did = $this->_post('did','','设备ID缺失');
        $name = $this->_post('name','','设备名称缺失');
        $token = $this->_post('token','');
        $device_model = $this->_post('device_model','','设备型号缺失');

        $entity = array(
            'uid' => $uid,
            'did' => $did,
            'name' => $name,
            'token' => $token,
            'device_model' => $device_model
        );

        $result = $this->partUpdate($entity);
        if($result['status']){
            $this->apiReturnSuc($result['info']);//返回设备编号
        }else{
            $this->apiReturnErr('添加失败');
        }



    }

    /**
     * 硬件设备删除
     */
    public function delete(){

        $this->checkVersion(100);
        addLog("Aq/delete",$_GET,$_POST,"应用" . $this->client_id . "调用Aq删除接口");

        $map = array(
            'uid' => $this->_post('uid','','用户ID不能为空'),
            'id' => $this->_post('id','','设备编号不能为空')
        );

        $result = apiCall(AqApi::DELETE,array($map));
        if($result['status']){
            $this->apiReturnSuc('删除成功');
        }else{
            $this->apiReturnErr('删除失败');
        }

    }

    /**
     * 硬件设备更新
     */
    public function update(){

        $this->checkVersion(100);
        addLog("Aq/update",$_GET,$_POST,"应用" . $this->client_id . "调用Aq更新接口");

        $id = $this->_post('id','','设备编号缺失');
        $uid = $this->_post('uid','','用户ID缺失');
        $did = $this->_post('did','');
        $name = $this->_post('name','');
        $token = $this->_post('token','');
        $device_model = $this->_post('device_model','');

        $entity = array(
            'id' => $id,
            'uid' => $uid,
            'did' => $did,
            'name' => $name,
            'token' => $token,
            'device_model' => $device_model
        );

        $result = $this->partUpdate($entity,true);
        if($result['status']){
            $this->apiReturnSuc('更新成功');
        }else{
            $this->apiReturnErr('更新失败');//返回设备编号
        }

    }

    /**
     * 硬件设备查询
     */
    public function query(){

        $this->checkVersion(100);
        addLog("Aq/query",$_GET,$_POST,"应用" . $this->client_id . "调用Aq查询接口");

        $map = array(
            'uid' => $this->_post('uid','','用户ID不能为空')
        );

        $order['create_time'] = "desc";

        $result = apiCall(AqApi::QUERY_NO_PAGING,array($map,$order));

        if($result['status']){
            $this->apiReturnSuc($result['info']);
        }else{
            $this->apiReturnErr('查询失败');
        }

    }


    /**
     * 更新部分数据,不存在则创建
     */
    private function partUpdate($entity,$only_save=false){
        $map = array(
            'uid'=> $entity['uid'],
            'id'=> $entity['id'],
            'did'=> $entity['did'],
        );
        foreach($map as $key=>$val){
            if(empty($val))unset($map[$key]);
        }
        $result = apiCall(AqApi::GET_INFO,array($map));

        if($result['status']){
            if(is_null($result['info'])){
                //该数据不存在,需要创建
                if(!$only_save){
                    $result = apiCall(AqApi::ADD,array($entity));
                    if($result['status']){
                        return array('status'=>true,'info'=>$result['info']);
                    }else{
                        return array('status'=>false,'info'=>$result['info']);
                    }
                }

            }else{
                //该数据存在,需要修改
                unset($entity['uid'],$entity['ddid'],$entity['id']);
                foreach($entity as $key=>$val){
                    if(empty($val))unset($entity[$key]);
                }
                $result = apiCall(AqApi::SAVE,array($map,$entity));
                if($result['status']){
                    return array('status'=>true,'info'=>'保存成功');
                }else{
                    return array('status'=>false,'info'=>$result['info']);
                }
            }



        }else{
            return array('status'=>false,'info'=>'保存更新失败');
        }


    }

}